package api

import (
	"gitee.com/chejiangyi/bsfgo/demo/domain"
	"gitee.com/chejiangyi/bsfgo/gin2/controller"
	"github.com/gin-gonic/gin"
	"time"
)

type RedisController struct {
	controller.BaseApiController
}

func (m *RedisController) Test(c *gin.Context) {
	defer m.CatchJsonError(c)
	exist := domain.RedisInstance.Exists("aaa")
	if exist == false {
		domain.RedisInstance.Set("aaa", "bbb", time.Minute*30)
	}
	aaa := domain.RedisInstance.Get("aaa")
	m.JsonSuccess(c, aaa)
}

func (m *RedisController) Lock(c *gin.Context) {
	defer m.CatchJsonError(c)
	lock := "lock"

	isLock := domain.RedisInstance.Lock(lock, "Lock Task", time.Second*60)
	defer func() {
		if isLock {
			domain.RedisInstance.Unlock(lock)
		}
	}()
	//do work
	m.JsonSuccess(c, isLock)
}
